﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：庄金冬 
    //
    // 文件名：BLL_Distribution.cs
    // 文件功能描述：配送操作业务逻辑类,,实现接口IBLL_Company,继承自类MarshalByRefObject
    // 创建标识：庄金冬2009-03-22
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Text;
using HZ.DrugTrade.IBLL;
using HZ;
using HZ.DrugTrade.Model;
using HZ.Common;

namespace HZ.DrugTrade.BLL
{

    [Serializable]
    /// <summary>
    /// 配送操作业务逻辑类,实现接口IBLL_Company,继承自类MarshalByRefObject
    /// </summary>
    public class BLL_Distribution : MarshalByRefObject, IBLL_Distribution
    {
        #region IDistribution Members


        /// <summary>
        /// 新增非中标产品配送关系
        /// </summary>
        /// <param name="distributionrelation"></param>
        /// <returns></returns>
        public int AddDistributionUnBid(HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            Common.ThrowHelper.Throw(distributionrelation != null);
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.ProcureCatalogID));
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.HospitalID));
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.CompanyID_PS));

            if (string.IsNullOrEmpty(distributionrelation.FlowID))
            {
                // 赋默认值1
                distributionrelation.FlowID = "1";

            }
            int result = DAL.DAL_DistributionRelation.AddDistributionUnBid(distributionrelation);
            if (result == 1) {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog() { OperateMessage = string.Format("新增非中标产品配送关系") }); 
            }
            return result;
        }

        /// <summary>
        /// 新增配送关系
        /// </summary>
        /// <param name="distributionrelation"></param>
        /// <returns></returns>
        public int AddDistribution(HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            Common.ThrowHelper.Throw(distributionrelation != null);
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.ProcureCatalogID));
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.HospitalID));
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.CompanyID_PS));

            if (string.IsNullOrEmpty(distributionrelation.FlowID))
            {
                // 赋默认值1
                distributionrelation.FlowID = "1";

            }
            int result = DAL.DAL_DistributionRelation.AddDistribution(distributionrelation);
            if (result == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog() { OperateMessage = string.Format("新增配送关系") });
            }
            return result;

            // return HZ.Data.DAL_Helper.OperateTable<HZ.DrugTrade.Model.DistributionRelation>(distributionrelation, HZ.Data.TableOperator.Insert);
        }

        /// <summary>
        /// 删除配送关系
        /// </summary>
        /// <param name="distributionrelation">配送关系【ProcureCatalogID，CompanyID_PS，HospitalID必填】</param>
        public int DelDistribution(HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            Common.ThrowHelper.Throw(distributionrelation != null);
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.ProcureCatalogID));
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.HospitalID));
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.CompanyID_PS));
            int state = DAL.DAL_DistributionRelation.DelDistribution(distributionrelation);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("删除配送关系：{0};处理时间:{1};)", distributionrelation.CompanyID_PS, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }

        /// <summary>
        /// 批量增加配送关系
        /// </summary>
        /// <param name="distributionrelationList">配送关系集合</param>
        public int AddDistribution_Batch(IList<HZ.DrugTrade.Model.DistributionRelation> distributionrelationList)
        {
            BLL_UserSession.CheckSession();
            int state = DAL.DAL_DistributionRelation.AddDistribution_Batch(distributionrelationList, 0, null);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("批量增加配送关系;处理时间:{0};)", DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }
        /// <summary>
        /// 批量增加配送关系(从采购目录及企业角度增加医疗机构)
        /// </summary>
        /// <param name="distributionrelationList">配送关系集合</param>
        /// <param name="distributionrelation">配送关系:ProcureCatalogID,CompanyID_PS必填</param>
        public int AddDistributionByProcureCatalogCompany_Batch(IList<HZ.DrugTrade.Model.DistributionRelation> distributionrelationList, HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            int state = DAL.DAL_DistributionRelation.AddDistribution_Batch(distributionrelationList, 1, distributionrelation);
            if (state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("批量增加配送关系(从采购目录及企业角度增加医疗机构):{1};处理时间:{0};)", DateTime.Now.ToString(), distributionrelation.CompanyID_PS), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }

        /// <summary>
        /// 批量增加配送关系(从采购目录及医疗机构角度增加企业)
        /// </summary>
        /// <param name="distributionrelationList">配送关系集合</param>
        /// <param name="distributionrelation">配送关系:ProcureCatalogID,HospitalID必填</param>
        public int AddDistributionByProcureCatalogHospital_Batch(IList<HZ.DrugTrade.Model.DistributionRelation> distributionrelationList, HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            int state = DAL.DAL_DistributionRelation.AddDistribution_Batch(distributionrelationList, 2, distributionrelation);
            if (state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("批量增加配送关系(从采购目录及企业角度增加医疗机构):{1};处理时间:{0};)", DateTime.Now.ToString(), distributionrelation.CompanyID_PS), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }

        /// <summary>
        /// 批量增加配送关系(从企业及医疗机构角度增加采购目录)
        /// </summary>
        /// <param name="distributionrelationList">配送关系集合</param>
        /// <param name="distributionrelation">配送关系:CompanyID_PS,HospitalID必填</param>
        public int AddDistributionByCompanyHospital_Batch(IList<HZ.DrugTrade.Model.DistributionRelation> distributionrelationList, HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            int state =  DAL.DAL_DistributionRelation.AddDistribution_Batch(distributionrelationList, 3, distributionrelation);
            if (state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("批量增加配送关系(从采购目录及企业角度增加医疗机构):{1};处理时间:{0};)", DateTime.Now.ToString(), distributionrelation.CompanyID_PS), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }


        //todo !!!!!医疗机构设置某个产品配送企业
        /// <summary>
        /// 医疗机构设置某个产品配送企业
        /// </summary>
        /// <param name="distributionrelationList">配送关系集合</param>
        ///  <param name="flag">0.不保存为默认配送关系1.保存为默认配送关系</param>
        public int SetDistributionRelation(IList<HZ.DrugTrade.Model.DistributionRelation> distributionrelationList, int flag)
        {
            BLL_UserSession.CheckSession();
            //return DAL.DAL_DistributionRelation.AddDistribution_Batch(distributionrelationList, 3, distributionrelation);
            return 1;
        }

        /// <summary>
        /// 更新配送关系
        /// </summary>
        /// <param name="distributionrelation">配送关系</param>
        public int UpdateDistribution(HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            if (distributionrelation.IsUsing.HasValue && distributionrelation.IsUsing.Value)
            {
                int count = (int)HZ.Data.DbHelperFactory.Create().ExecuteScalar(string.Format(@"SELECT COUNT(*) FROM TB_DistributionRelation WHERE HospitalID = '{0}' AND ProcureCatalogID = '{1}' AND IsUsing = 1", distributionrelation.HospitalID, distributionrelation.ProcureCatalogID));
                if (count > 0)
                {
                    throw new Exception("已经存在默认配送企业不能同时设置2个默认配送企业");
                }

            }
            int state = HZ.Data.DAL_Helper.OperateTable<HZ.DrugTrade.Model.DistributionRelation>(distributionrelation, HZ.Data.TableOperator.Update);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("更新配送关系：{0};处理时间:{1};)", distributionrelation.CompanyID_PS, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state; 
        }

        /// <summary>
        /// 获取ProcureCatalogID的所有配送关系
        /// </summary>
        /// <param name="ProcureCatalogID">采购目录编号</param>
        /// <param name="Hospital">医疗机构实体</param>
        /// <returns>可配送企业列表</returns>
        public IList<Company> GetDistributionRelationByHospital(string ProcureCatalogID, User Hospital)
        {
            BLL_UserSession.CheckSession();
            if (BLL_Cache.AreaDistributionCache.Keys.Contains(ProcureCatalogID) && BLL_Cache.AreaDistributionCache[ProcureCatalogID].Keys.Contains(Hospital.Org.OrgID))
            {
                var o = BLL_Cache.AreaDistributionCache[ProcureCatalogID][Hospital.Org.OrgID];
                IList<Company> company = new List<Company>();
                foreach (var item in o)
                    company.Add(new Company() { CompanyID = item.CompanyID_PS, CompanyName = BLL_Cache.CompanyCache[item.CompanyID_PS].CompanyName, CompanyType = BLL_Cache.CompanyCache[item.CompanyID_PS].CompanyType });
                return company;
            }
            else
            {
                return null;
            }


            // return DAL.DAL_AreaDistribution.GetDistributionRelationByHospital(ProcureCatalogID, Hospital.Org.OrgID);

        }

        /// <summary>
        /// 更新交易价格
        /// </summary>
        /// <param name="distributionrelation">配送关系</param>
        public int UpdateDistribution_CompanyPrice(HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            Common.ThrowHelper.Throw(distributionrelation != null);
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.ProcureCatalogID));
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.HospitalID));
            Common.ThrowHelper.Throw(distributionrelation.Price != null);
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.CompanyID_PS));

            Model.DistributionRelation updatemodel = HZ.Data.DAL_Helper.GetModelByID(distributionrelation);
            updatemodel.CompanyID_PS = distributionrelation.CompanyID_PS;
            updatemodel.Price = distributionrelation.Price;
            updatemodel.IsUsing = true;
            int state = HZ.Data.DAL_Helper.OperateTable(updatemodel, HZ.Data.TableOperator.Update);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("更新交易价格：{0};处理时间:{1};)", distributionrelation.CompanyID_PS, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }

        /// <summary>
        /// 撤废/取消撤废配送关系
        /// </summary>
        /// <param name="distributionrelation">配送关系</param>
        /// <returns></returns>
        public int UpdateDistribution_IsUsing(HZ.DrugTrade.Model.DistributionRelation distributionrelation)
        {
            BLL_UserSession.CheckSession();
            Common.ThrowHelper.Throw(distributionrelation != null);
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.ProcureCatalogID));
            Common.ThrowHelper.Throw(!string.IsNullOrEmpty(distributionrelation.HospitalID));
            Common.ThrowHelper.Throw(distributionrelation.IsUsing != null);
            Model.DistributionRelation updatemodel = HZ.Data.DAL_Helper.GetModelByID(distributionrelation);
            updatemodel.IsUsing = distributionrelation.IsUsing;
            if (distributionrelation.IsUsing.HasValue && distributionrelation.IsUsing.Value)
            {
                int count = (int)HZ.Data.DbHelperFactory.Create().ExecuteScalar(string.Format(@"SELECT COUNT(*) FROM TB_DistributionRelation WHERE HospitalID = '{0}' AND ProcureCatalogID = '{1}' AND IsUsing = 1", distributionrelation.HospitalID, distributionrelation.ProcureCatalogID));
                if (count > 0)
                {
                    throw new Exception("已经存在默认配送企业不能同时设置2个默认配送企业");
                }

            }
            int state = HZ.Data.DAL_Helper.OperateTable(updatemodel, HZ.Data.TableOperator.Update);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("撤废/取消撤废配送关系：{0};处理时间:{1};)", distributionrelation.CompanyID_PS, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }

        /// <summary>
        /// 条件获取配送关系
        /// </summary>
        /// <param name="distribution">查询实体</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页条数</param>
        /// <param name="totalrecord">总条数</param>
        public IList<HZ.DrugTrade.Model.View.DistributionRelation> GetDistributionByWhere(HZ.DrugTrade.Model.Query.DistributionRelation distribution, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_DistributionRelation.GetDistributionByWhere(distribution, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 获取未在采购单明细的配送关系
        /// </summary>
        /// <param name="distribution">配送关系查询</param>
        /// <param name="orderdetail">采购单明细</param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.DistributionRelation> GetDistributionByWhereNotInCurrentPurchaseOrder(HZ.DrugTrade.Model.Query.DistributionRelation distribution, IList<HZ.DrugTrade.Model.View.PurchaseOrderDetail> orderdetail, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            foreach (var item in orderdetail)
            {
                HZ.Common.ThrowHelper.Throw(item.ProcureCatalogID != null, "ProcureCatalogID不能空");
            }
            string instr = HZ.Data.DAL_Helper.GetModelFieldArrayString(orderdetail, "ProcureCatalogID", ",", true);
            if (!string.IsNullOrEmpty(instr))
            {

                instr = string.Format("ProcureCatalogID NOT IN ({0})", instr);
                if (!string.IsNullOrEmpty(distribution.Append))
                {
                    distribution.Append = distribution.Append + " and " + instr;
                }
                else
                {
                    distribution.Append = instr;
                }
            }
            return GetDistributionByWhere(distribution, currentpage, pagesize, out totalrecord);

        }
        /// <summary>
        ///  条件获取配送(已确认配且已送尚未验收)情况信息
        /// </summary>
        /// <param name="distribution">查询实体</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页条数</param>
        /// <param name="totalrecord">总条数</param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.Distribution> GetDistributionByWhere(HZ.DrugTrade.Model.Query.Distribution distribution, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_DistributionRelation.GetDistributionByWhere(distribution, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        ///  条件获取未配送(已确认尚未配送)信息
        /// </summary>
        /// <param name="distribution">查询实体</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页条数</param>
        /// <param name="totalrecord">总条数</param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.Distribution> GetNoDistributionByWhere(HZ.DrugTrade.Model.Query.Distribution distribution, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_DistributionRelation.GetNoDistributionByWhere(distribution, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        ///条件获取采购单明细配送单情况信息（DistributeState：0.待验收；1.已验收）
        /// </summary>
        /// <param name="distribution">查询实体</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页条数</param>
        /// <param name="totalrecord">总条数</param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.Distribution> GetNoWarehouseByWhere(HZ.DrugTrade.Model.Query.Distribution distribution, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_DistributionRelation.GetNoWarehouseByWhere(distribution, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        ///  条件获取已入库（验收）信息
        /// </summary>
        /// <param name="distribution">查询实体</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页条数</param>
        /// <param name="totalrecord">总条数</param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.Distribution> GetWarehouseByWhere(HZ.DrugTrade.Model.Query.Distribution distribution, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_DistributionRelation.GetWarehouseByWhere(distribution, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 条件查询【地区医疗机构采购目录】
        /// </summary>
        /// <param name="where">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.AreaHospitalProcureCatalo> GetAreaHospitalProcureCatalogByWhere(HZ.DrugTrade.Model.Query.AreaHospitalProcureCatalo where, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_HospitalProcureCatalog.GetAreaHospitalProcureCatalogByWhere(where, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 条件查询【地区医疗机构】[可根据HospitalName，AreaID(省级-省级编号,市级-市级编号)查询]
        /// </summary>
        /// <param name="where">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.AreaHospitalProcureCatalo> GetAreaHospitalByWhere(HZ.DrugTrade.Model.Query.AreaHospitalProcureCatalo where, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_HospitalProcureCatalog.GetAreaHospitalByWhere(where, currentpage, pagesize, out totalrecord);
        }

        #endregion


        #region 转配送部分
        /// <summary>
        /// 批量增加转配送日志
        /// </summary>
        /// <param name="loglist">转配送记录</param>
        /// <returns>(1成功0失败)</returns>
        public int AddTransformDistributionLog(IList<TransformDistributionLog> loglist)
        {
            BLL_UserSession.CheckSession();
            ThrowHelper.Throw(loglist != null, "loglist != null");
            ThrowHelper.Throw(loglist.Count > 0, "loglist.Count > 0");
            foreach (var item in loglist)
            {
                ThrowHelper.Throw(item.AreaID != null, "item.AreaID != null");
                ThrowHelper.Throw(item.LastCompanyID != null, "item.LastCompanyID != null");
                ThrowHelper.Throw(item.NowCompanyID != null, "item.NowCompanyID != null");
                ThrowHelper.Throw(item.ProcureCatalogID != null, "item.ProcureCatalogID != null");
                if (item.LogSerialID == null)
                {
                    item.LogSerialID = Guid.NewGuid().ToString();
                }
                item.IsEndNode = true;
                item.TransformDistributeTime = DateTime.Now;
            }
            int state = DAL.DAL_TransformDistributionLog.AddTransformDistributionLog_Batch(loglist);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("批量增加转配送日志;处理时间:{0};)", DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;

            throw new NotImplementedException();
        }

        /// <summary>
        /// 查询转配送记录
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        IList<HZ.DrugTrade.Model.View.TransformDistributionLog> GetDistributionLog(Model.Query.TransformDistributionLog where, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<HZ.DrugTrade.Model.View.TransformDistributionLog, Model.Query.TransformDistributionLog>(where, currentpage, pagesize, out totalrecord);

            throw new NotImplementedException();
        }
        #endregion


        #region 分配送部分

        /// <summary>
        /// 批量增加分配送日志
        /// </summary>
        /// <param name="loglist">分配送记录</param>
        /// <returns>0添加失败，1添加成功，2数据重复</returns>
        public int AddPartDistributionLog(IList<PartDistributionLog> loglist)
        {
            BLL_UserSession.CheckSession();
            ThrowHelper.Throw(loglist != null, "loglist != null");
            ThrowHelper.Throw(loglist.Count > 0, "loglist.Count > 0");
            foreach (var item in loglist)
            {
                ThrowHelper.Throw(item.AreaID != null, "item.HospitalID != null");
                ThrowHelper.Throw(item.LastCompanyID != null, "item.LastCompanyID != null");
                ThrowHelper.Throw(item.NowCompanyID != null, "item.NowCompanyID != null");
                ThrowHelper.Throw(item.ProcureCatalogID != null, "item.ProcureCatalogID != null");
                if (item.PartDistributionSerialID == null)
                {
                    item.PartDistributionSerialID = Guid.NewGuid().ToString();
                }
                item.IsEndNode = true;
                item.PartDistributeTime = DateTime.Now;
            }
            int state =  DAL.DAL_PartDistributionLog.AddPartDistributionLog(loglist);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("批量增加分配送日志;处理时间:{0};)", DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取医疗机构某采购目录的可用的配送企业
        /// </summary>
        /// <param name="ProcureCatalogID"></param>
        /// <param name="HospitalID"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.AreaDistribution> GetPartDistribution(string ProcureCatalogID, string HospitalID, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            ThrowHelper.Throw(!string.IsNullOrEmpty(ProcureCatalogID), "ProcureCatalogID不能空");
            ThrowHelper.Throw(!string.IsNullOrEmpty(HospitalID), "HospitalID不能空");
            return DAL.DAL_PartDistributionLog.GetPartDistribution(ProcureCatalogID, HospitalID, currentpage, pagesize, out  totalrecord);

        }


        /// <summary>
        /// 获取分配送配送记录情况
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.Distribution> GetPartDistribution(HZ.DrugTrade.Model.Query.Distribution where, string fathercompanyID, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            ThrowHelper.Throw(!string.IsNullOrEmpty(fathercompanyID), "fathercompanyID不能空");
            return DAL.DAL_PartDistributionLog.GetPartDistribution(where, fathercompanyID, currentpage, pagesize, out totalrecord);
            throw new NotImplementedException();
        }


        #endregion


        #region IBLL_Distribution Members


        IList<HZ.DrugTrade.Model.View.TransformDistributionLog> IBLL_Distribution.GetDistributionLog(HZ.DrugTrade.Model.Query.TransformDistributionLog where, int currentpage, int pagesize, out int totalrecord)
        {
            return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<HZ.DrugTrade.Model.View.TransformDistributionLog, HZ.DrugTrade.Model.Query.TransformDistributionLog>(where, currentpage, pagesize, out totalrecord);
            throw new NotImplementedException();
        }


        /// <summary>
        /// 获取地区分配送信息
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.AreaDistribution> GetAreaDistribution(HZ.DrugTrade.Model.Query.AreaDistribution where, int currentpage, int pagesize, out int totalrecord)
        {
            //使用索引优化  请确保该视图建立索引架构 WITH SCHEMABINDING 
            return DAL.DAL_AreaDistribution.GetAreaDistribution(where, currentpage, pagesize, out totalrecord);
            // return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<HZ.DrugTrade.Model.View.AreaDistribution, HZ.DrugTrade.Model.Query.AreaDistribution>(where, currentpage, pagesize, out totalrecord);
        }
        /// <summary>
        /// 主配送获取可管理配送产品
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        /// 获取企业可管理产品使用 View_ProcureCatalog by lig 20091115
        //public IList<HZ.DrugTrade.Model.View.DistributionManage> GetAreaDistributionManage(HZ.DrugTrade.Model.Query.DistributionManage where, int currentpage, int pagesize, out int totalrecord)
        //{
        //    ThrowHelper.Throw(!string.IsNullOrEmpty(where.CompanyID_Main), "主配送不能空");
        //    return DAL.DAL_AreaDistribution.GetAreaDistributionManage(where, currentpage, pagesize, out totalrecord);
        //    //NOTE:Modified by ligang 2009-08-22
        //    //return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<HZ.DrugTrade.Model.View.DistributionManage, HZ.DrugTrade.Model.Query.DistributionManage>(where, currentpage, pagesize, out totalrecord);
        //}
        #endregion

        #region IBLL_Distribution Members

        /// <summary>
        /// 获取转配送给别人的产品列表信息
        /// </summary>
        /// <param name="companyID">当前企业ID</param>
        /// <param name="where"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<Model.View.ProcureCatalog> GetTransformProcureCatalogTo(string companyID, HZ.DrugTrade.Model.Query.ProcureCatalog where, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL.DAL_AreaDistribution.GetTransformProcureCatalogTo(companyID, where, currentpage, pagesize, out totalrecord);
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取别人转配送给我的产品列表信息
        /// </summary>
        /// <param name="companyID">>当前企业ID</param>
        /// <param name="where"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<Model.View.ProcureCatalog> GetTransformProcureCatalogFrom(string companyID, HZ.DrugTrade.Model.Query.ProcureCatalog where, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL.DAL_AreaDistribution.GetTransformProcureCatalogFrom(companyID, where, currentpage, pagesize, out totalrecord);
            throw new NotImplementedException();
        }
        /// <summary>
        /// 往下追溯转配送记录
        /// (追溯转配送给谁了)
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.TransformDistributionLog> GetDistributionLogDown(string companyID, string areaID, string ProcureCatalogID)
        {
            return DAL.DAL_AreaDistribution.GetDistributionLogDown(companyID, areaID, ProcureCatalogID);
        }
        /// <summary>
        /// 往上追溯转配送记录
        /// (追溯转谁转配送给我的)
        /// </summary>
        /// <param name="companyID"></param>
        /// <param name="areaID"></param>
        /// <param name="ProcureCatalogID"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.TransformDistributionLog> GetDistributionLogUp(string companyID, string areaID, string ProcureCatalogID)
        {
            return DAL.DAL_AreaDistribution.GetDistributionLogUp(companyID, areaID, ProcureCatalogID);
        }



        /// <summary>
        /// 批量删除地区配送关系
        /// </summary>
        /// <param name="DeleteList"></param>
        /// <returns></returns>
        public int DeleteAreaDistribution_Batch(IList<AreaDistribution> DeleteList)
        {
            int state = DAL.DAL_AreaDistribution.DeleteAreaDistribution_Batch(DeleteList);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("批量删除地区配送关系;处理时间:{0};)", DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }



        /// <summary>
        /// 获取医疗机构默认
        /// </summary>
        /// <param name="distribution"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.DistributionRelation> GetHospitalProcureCatalog_With_DefaultDistribution_Company(HZ.DrugTrade.Model.Query.DistributionRelation distribution, int currentpage, int pagesize, out int totalrecord)
        {
            ThrowHelper.Throw(distribution.HospitalID != null);
            IList<HZ.DrugTrade.Model.View.DistributionRelation> list = DAL.DAL_DistributionRelation.GetHospitalProcureCatalog_With_DefaultDistribution_Company(distribution, currentpage, pagesize, out totalrecord);

            if (BLL_Cache.UseArea)
            {
                foreach (var item in list)
                {
                    if (BLL_Cache.AreaDistributionCache.Keys.Contains(item.ProcureCatalogID) && BLL_Cache.AreaDistributionCache[item.ProcureCatalogID].Keys.Contains(item.AreaID))
                    {


                        IList<Model.AreaDistribution> ilAreaDistribution = BLL_Cache.AreaDistributionCache[item.ProcureCatalogID][item.AreaID];
                        item.CompanyIDs_PS = HZ.Data.DAL_Helper.GetModelFieldArrayString(ilAreaDistribution, "CompanyID_PS", ",", false);
                        item.CompanyNames_PS = BLL_Cache.GetCompanyNamesFromIDs(item.CompanyIDs_PS);
                    }
                }
            }

            return list;

        }

        /// <summary>
        /// 获取企业被采购目录
        /// </summary>
        /// <param name="where"></param>
        /// <param name="CurrentPage"></param>
        /// <param name="PageSize"></param>
        /// <param name="TotalRecord"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.CompanyProcureCatalog> GetCompanyProcureCatalog(HZ.DrugTrade.Model.Query.CompanyProcureCatalog where, int CurrentPage, int PageSize, out int TotalRecord)
        {
            return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<HZ.DrugTrade.Model.View.CompanyProcureCatalog, HZ.DrugTrade.Model.Query.CompanyProcureCatalog>(where, CurrentPage, PageSize, out TotalRecord);
            throw new NotImplementedException();
        }
        #endregion

    }
}

